package com.pignest.hotel.ajax;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;

import com.pignest.framework.json.JsonArray;
import com.pignest.framework.json.JsonNumber;
import com.pignest.framework.json.JsonObject;
import com.pignest.hotel.beans.Card;
import com.pignest.hotel.beans.Log;
import com.pignest.hotel.beans.User;
import com.pignest.hotel.beans.global.ClientSession;
import com.pignest.hotel.service.AdminService;
import com.pignest.hotel.service.UserService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Config;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.system.L;
import com.pignest.hotel.util.DateUtil;

@RemoteProxy
public class GlobalAjax extends HotelAjax {
	
	@RemoteMethod
	public boolean logout() {
		Hotel.logout(getRequest(), getResponse());
		return true;
	}

	@RemoteMethod
	public boolean login(String workNumber, String password) {
//		if (StringUtils.isNotEmpty(cardNumber)) {
//			UserService service = Services.getUserService();
//			User user = service.getUserByCardNumber(cardNumber);
//			return user != null && login(user.getWorkNumber(), user.getPassword(), null);
//		}
		return Hotel.login(getRequest(), getResponse(), workNumber, password, false);
	}
	
	@RemoteMethod
	public boolean checkSession() {
		return Hotel.getCurrentUser() != null;
	}

	
	@RemoteMethod
	public boolean initializeUser() {
		return Services.getUserService().insertSuperUser();
	}
	
	@RemoteMethod
	public String loginByCard(String serialNumber) {
		Card card = Services.getCardService().getUsableUserCard(serialNumber);
		if (card == null) {
			return "员工卡无效, 或者 已经被禁用";
		}
		//User user = Services.getUserService().getUserByCardNumber(serialNumber);
		User user = Services.getUserService().getUserByCardNumber(card.getCardNumber());
		if (!user.isEnable())
			return user.getName() + "已经被禁卡登录";
		if (login(user.getWorkNumber(), user.getPassword()))
			return null;
		return "登录失败; 如果你的初始密码暂未修改, 无法通过刷卡登录";
	}
	
	@RemoteMethod
	public User getUserByCardSerialNumber(String serialNumber) {
		Card card = Services.getCardService().getCardBySerialNumber(serialNumber);
		return card == null ? null : Services.getUserService().getUserByCardNumber(card.getCardNumber());
	}
	
	@RemoteMethod
	public User getUserByCardNumber(String cardNumber) {
		return Services.getUserService().getUserByCardNumber(cardNumber);
	}
	
	@RemoteMethod
	public String updateProfile(String nickName, String gender, String phone, String birthday) {
		ClientSession cs = getClientSession();
		if (cs == null)
			return "请先登录!";
		User user = cs.getUser();
		if (user == null) {
			return "请先登录!";
		}
		Date birth = null;
		if (StringUtils.isNotEmpty(birthday)) {
			birth = DateUtil.toDate(birthday, DateUtil.DEFAULT_DATE_FORMAT);
			if (birth == null) {
				return "请输入正确的生日格式: yyyy-mm-dd";
			}
		}
		UserService service = Services.getUserService();
		user = service.get(User.class, user.getId());
		user.setNickName(nickName);
		user.setPhone(phone);
		user.setBirthday(birth);
		user.setGender(gender);
		service.update(user);
		cs.updateUser(user);
		L.update(user, "更新用户信息");
		return null;
	}
	
	@RemoteMethod
	public String updatePassword(String oldPass, String newPass) {
		if (StringUtils.isEmpty(oldPass))
			return "请输入当前密码!";
		if (StringUtils.isEmpty(newPass))
			return "请输入新密码!";
		ClientSession cs = getClientSession();
		if (cs == null)
			return "请先登录!";
		User user = cs.getUser();
		if (user == null)
			return "请先登录!";
		String pass = user.getPassword();
		if (StringUtils.isEmpty(pass))
			pass = Config.getDefaultPassword();
		if (!pass.equals(oldPass)){
			return "当前密码输入错误!";
		}
		UserService service = Services.getUserService();
		user = service.get(User.class, user.getId());
		user.setPassword(newPass);
		service.update(user);
		cs.updateUser(user);
		L.update(user, "修改用户密码");
		return null;
	}
	
	@RemoteMethod
	public String listLogs(String userName, String startTime, String endTime, String ip, String keyword, int pageSize, int pageIndex) {
		Date start = DateUtil.toDate(startTime);
		Date end = DateUtil.toDate(endTime);
		AdminService service = Services.getAdminService();
		int count = service.countLogs(userName, start, end, ip, keyword);
		List<Log> logs = count > 0 ? service.listLogs(userName, start, end, ip, keyword, "time asc", pageSize, pageIndex) : null;
		JsonObject r = new JsonObject();
		r.put("iTotalRecords", new JsonNumber(count));
		if (logs == null)
			return r.toExternalRepresentation();
//		r.put("aoColumns", getComumns());
		JsonArray aaData = new JsonArray();
		for (Log log : logs) {
			JsonArray l = new JsonArray();
			l.add(log.getUserName(), DateUtil.toString(log.getTime()), log.getIp(), log.getAction(), log.getCategory(), log.getDescription());
			aaData.add(l);
		}
		r.put("aaData", aaData);
		System.out.println(r.toExternalRepresentation());
		return r.toExternalRepresentation();
	}
	
//	private JsonArray getComumns() {
//		JsonArray cols = new JsonArray();
//		cols.add(new JsonObject("sTitle", "用户名"));
//		cols.add(new JsonObject("sTitle", "时间"));
//		cols.add(new JsonObject("sTitle", "IP地址"));
//		cols.add(new JsonObject("sTitle", "类别"));
//		cols.add(new JsonObject("sTitle", "动作"));
//		cols.add(new JsonObject("sTitle", "描述"));
//		return cols;
//	}
}
